package com.hhsjdesign.dynamicui.core.property.viewdata

import android.content.Context
import android.graphics.*
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RoundRectShape
import android.util.ArrayMap
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.hhsjdesign.dynamicui.utils.ThemeColorUtil
import com.hhsjdesign.dynamicui.utils.ViewTreeTransformer
import com.hhsjdesign.dynamicui.annotation.ViewTypeCode
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.property.item.ImageViewProperty
import com.hhsjdesign.dynamicui.core.property.item.TextViewProperty
import com.hhsjdesign.dynamicui.core.property.ViewBaseProperty
import com.hhsjdesign.dynamicui.reddot.RedDotFrameLayout
import com.hhsjdesign.dynamicui.reddot.RedDotLinearLayout
import org.json.JSONObject

//用于解析item树，可以解析除视图的层级结构，元素关系
open class ItemViewData {
    //父视图类型 Linearlayout , FrameLayout
    var parentType: String? = null

    //视图类型
    lateinit var type: String

    //视图别名
    lateinit var name: String

    //树深度
    var deep: Int = 0;

    //index
    var index: Int = 0;

    //子视图树
    var treeList: MutableList<ItemViewData>? = null

    //视图属性
    var property: ViewBaseProperty? = null

    var viewTag: String? = null



    /**
     * 整个view的属性
     * 1.解析tag 用于业务
     * 2.解析属性 用于视图
     */
    fun injectProperty(propertyMap: JSONObject) {
        val jo = propertyMap[name] as JSONObject
        viewTag = jo.optString("fun", null)
        property = ViewBaseProperty.fromJSON(jo)
    }

    //绑定定视图
    open fun bindData(iViewable: IViewable<*>, view: Map<String, View>) {
        val clazz = iViewable.businessData
    }

    open class ViewTree(val contentView: View) {
        lateinit var map: ArrayMap<String, View>
    }

    /**
     * 返回一个View以及按顺序的map集合，方便从中找到对应的控件
     * @return 返回视图
     */
    open fun createView(context: Context): View {
        //1创建视图
        val contentView = ViewTreeTransformer.toViewTree(context, this).apply {
            if (type == ITEM_VIEW_TYPE_LINEAR_H) {
                (this as LinearLayout).orientation = LinearLayout.HORIZONTAL
            } else if (type == ITEM_VIEW_TYPE_LINEAR_V) {
                (this as LinearLayout).orientation = LinearLayout.VERTICAL
            }
        }

        //2.设置属性
        property?.let {

            //2.设置padding
            contentView.setPadding(
                it.padding[0],
                it.padding[1],
                it.padding[2],
                it.padding[3]
            )

            //3.设置layoutParams
            contentView.layoutParams = ViewTreeTransformer.marginLayout(parentType, it).apply {
                this.setMargins(
                    it.margin[0],
                    it.margin[1],
                    it.margin[2],
                    it.margin[3]
                )
            }

            //4.子视图的内部元素排列
            if (contentView is LinearLayout) {
                contentView.gravity = it.viewGravity()
            }

            //5.背景
//            contentView.setBackgroundColor(it.solidInt)
            var colorFilter: ColorMatrixColorFilter? = null
            if (ThemeColorUtil.SILENT) {
                val colorMatrix = ColorMatrix(
                    floatArrayOf(
                        0.229f, 0.587f, 0.114f, 0f, 0f,
                        0.229f, 0.587f, 0.114f, 0f, 0f,
                        0.229f, 0.587f, 0.114f, 0f, 0f,
                        0f, 0f, 0f, 1f, 0f
                    )
                )
                colorFilter = ColorMatrixColorFilter(colorMatrix)
            }

            contentView.background = ShapeDrawable(object : RoundRectShape(
                floatArrayOf
                    (
                    it.radius[0], it.radius[0],
                    it.radius[1], it.radius[1],
                    it.radius[2], it.radius[2],
                    it.radius[3], it.radius[3]
                ),
                RectF(0f, 0f, 0f, 0f),
                floatArrayOf
                    (
                    it.radius[0], it.radius[0],
                    it.radius[1], it.radius[1],
                    it.radius[2], it.radius[2],
                    it.radius[3], it.radius[3]
                )
            ) {
                override fun draw(canvas: Canvas?, paint: Paint?) {
                    super.draw(canvas, paint)
                    paint?.color = it.solidInt
                    if (ThemeColorUtil.SILENT)
                        colorFilter?.apply {
                            paint?.colorFilter = this
                        }
                }
            })
        }

        //6.其他特殊属性
        if (contentView is TextView && property is TextViewProperty) {
            //text
            contentView.setTextColor((property as TextViewProperty).textColor)
            contentView.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                (property as TextViewProperty).textSize
            )
            contentView.gravity = property!!.viewGravity()
        } else if (contentView is ImageView && property is ImageViewProperty) {
            //imageView
            contentView.scaleType = (property as ImageViewProperty).scaleType()
        }
        //7.子视图列表，迭代操作
        if (treeList != null && contentView is ViewGroup) {
            for (child in treeList!!) {
                contentView.addView(child.createView(context))
            }
        }
        return contentView
    }

    companion object {
        //子视图模型，由基本视图和布局组成
        const val ITEM_VIEW_TYPE_FRAME = "frame"
        const val ITEM_VIEW_TYPE_LINEAR_H = "linear_h"
        const val ITEM_VIEW_TYPE_LINEAR_V = "linear_v"
        const val ITEM_VIEW_TYPE_TEXT = "text"
        const val ITEM_VIEW_TYPE_IMAGE = "image"
        const val ITEM_VIEW_TYPE_VIEW = "view"

        private val map: Map<String, Class<*>> = mapOf(
            Pair(ITEM_VIEW_TYPE_FRAME, RedDotFrameLayout::class.java),
            Pair(ITEM_VIEW_TYPE_LINEAR_H, RedDotLinearLayout::class.java),
            Pair(ITEM_VIEW_TYPE_LINEAR_V, RedDotLinearLayout::class.java),
            Pair(ITEM_VIEW_TYPE_IMAGE, ImageView::class.java),
            Pair(ITEM_VIEW_TYPE_TEXT, TextView::class.java),
            Pair(ITEM_VIEW_TYPE_VIEW, View::class.java),
        )

        //获取class
        @JvmStatic
        fun viewClass(@ViewTypeCode type: String): Class<*> {
            return map[type] ?: View::class.java
        }
    }

}